/*
 * Copyright (c) 2006, 2021, webrx.cn All rights reserved.
 *
 */
package cn.webrx.wxs.config;

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.DispatcherServlet;

import javax.servlet.DispatcherType;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import java.util.EnumSet;

/**
 * <p>Project: wxshop - WebInitializer
 * <p>Powered by webrx On 2021-04-16 18:47:04
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 15
 */
public class WebInitializer implements WebApplicationInitializer {
    public void onStartup(ServletContext sc) throws ServletException {
        var context = new AnnotationConfigWebApplicationContext();
        //web.xml 加载springmvc.xml
        context.register(WebMvcConfig.class);
        //web.xml 加载 applicationContext.xml
        context.register(ApplicationContextConfig.class);
        DispatcherServlet servlet = new DispatcherServlet(context);
        //在启动web项目注册DispatcherServlet
        ServletRegistration.Dynamic registration = sc.addServlet("dispatcher", servlet);
        registration.setLoadOnStartup(1);
        registration.addMapping("/*");

        //注册编码过滤器 过滤地址：可以解决PrintWriter输出中文乱码 或 post提交中文乱码
        var cf = new CharacterEncodingFilter("utf-8", true);
        var regf = sc.addFilter("cf", cf);
        regf.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD), true, "/demo7","/demo6");


    }
}